package com.example.servlet.user;

import com.alibaba.fastjson.JSON;
import com.example.model.User;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;
import com.example.vo.Page;
import com.example.vo.ResponseData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user/selectByCondition")
public class SelectServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=utf-8");
        //获取用户列表页面中用户名输入框的值
        String username = req.getParameter("username");
        //获取用户列表所选择的状态
        String state = req.getParameter("state");
        //获取当前页的页码
        String currentPage = req.getParameter("currentPage");
        //获取每一页显示多少条数据
        String pageSize = req.getParameter("pageSize");
        UserService userService = new UserServiceImpl();
        Map<String,String> params = new HashMap<>();
        params.put("username",username);
        params.put("state",state);
        params.put("currentPage",currentPage);
        params.put("pageSize",pageSize);
        Page<User> page = userService.selectPageByCondition(params);
        resp.getWriter().println(JSON.toJSONString(ResponseData.ok(page)));
    }
}
